package com.jiyun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.entity.Game;
import com.jiyun.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Game)表控制层
 *
 * @author makejava
 * @since 2020-11-12 14:04:38
 */
@Controller
public class GameController {
    /**
     * 服务对象
     */
    @Autowired
    private GameService gameService;
    @ResponseBody
    @RequestMapping("searchGame")
    public Map<String ,Object> searchGame(
            @RequestParam(value = "page",defaultValue = "1")int pn,
            @RequestParam(value = "rwos",defaultValue = "2")int ps,
            String name
    ){
        PageHelper.startPage(pn,ps);
        List<Game> list = gameService.searchGame(name);
        PageInfo<Game> pg = new PageInfo<>(list);
        Map<String ,Object> map = new HashMap<>();
        map.put("total",pg.getTotal());
        map.put("rows",pg.getList());
        return map;
    }
    @RequestMapping("deleteById")
    public String deleteById(Integer id){
        gameService.deleteById(id);
        return "redirect:/show.jsp";
    }
    @RequestMapping("toUpdate")
    public String toUpdate(Integer id, Model mo){
        Game game = gameService.queryById(id);
        String[] cmp = game.getCmp().split("-");
        mo.addAttribute("cmp",cmp);
        mo.addAttribute("game",game);
        return "update";
    }
    @ResponseBody
    @RequestMapping("checkName")
    public String checkName(String name){
        Integer integer = gameService.checkName(name);
        if (integer!=null){
            return "true";
        }else{
            return "false";
        }
    }
    @RequestMapping("add")
    public String add(Game game, MultipartFile imgFile,String a,String b) throws IOException {
        String filename = imgFile.getOriginalFilename();
        if (filename != "") {
            imgFile.transferTo(new File("G:/img/"+filename));
            game.setImg(filename);
        }
        game.setCmp(a+"-"+b);
        gameService.insert(game);
        return "redirect:/show.jsp";
    }
    @RequestMapping("update")
    public String update(Game game, MultipartFile imgFile,String a,String b) throws IOException {
        String filename = imgFile.getOriginalFilename();
        if (filename != "") {
            imgFile.transferTo(new File("G:/img/"+filename));
            game.setImg(filename);
        }
        game.setCmp(a+"-"+b);
        gameService.update(game);
        return "redirect:/show.jsp";
    }
}